This page last changed on Feb 05, 2010 by brian.

JEE6 Servlet 3.0

Notes from https://www.sun.com/offers/details/java_ee6_servlet.xml

  • Added annotations for declarative programming and generics:
    /*
      You can use web.xml to override values
     */
    @WebServlet // defines a servlet
    @WebFilter // defines a filter
    @WebListener // defines a listener
    @WebInitParam // defines an init param
    @ServletSecurity // security constraints
    @MultipartConfig // file upload
  • @WebServlet
    • MUST specifiy url mapping
    • Default name of servlet is full qualified class name
    • Must still extend HttpServlet
    • Example:
      package com.foo;
      @WebServlet(name="MyServlet", urlPattern="/myApp/*")
      public class MyServlet extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse res) { ... }
      }
  • Enable use of frameworks without alot of boiler plate configuration. Modularize web.xml
    • Dynamic registration of Servlets and Filters
      • Performed during ServletContext intialization;
        • ServletContext.add<Servlet|Filter>
        • ServletContext.create<Servlet|Filter>
        • ServletContext.find<Servlet|Filter>
    • Example:
      // Registration
      ServletRegistration.Dynamic dynamic = 
          servletContext.addServlet("DynamicServlet", "com.foo.MyServlet");
      dynamic.addMapping("/dynamicServlet");
      dynamic.setAsyncSupported(true);
      
      // Lookup
      ServletRegistration declared = servletContext.getServletRegistration("DeclaredServlet");
      declared.addMapping("/declaredServlet");
      declared.setInitParameter("param", "value");
  • Modularized web.xml
    • web-fragment.xml
    • bundled in framework jar fil in META-INF
    • Almost identical to web.xml
    • Only jars in WEB-INF/lib will be scanned for web-fragment.xml files
    • Example:
      <web-fragment>
        <servlet>
          <servlet-name>welcome</servlet-name>
          ...
      </web-fragment>
    • Ordering is from JSF.
      • web.xml may declare ordering of fragments via <absolute-ordering>
      • fragments can specify ordering with <before> and <after>
  • ServletContainerInitializer
    • Expresses interest in classes via @HandleTypes
    • Container scans webapp for classes that match @HandleTypes and passes them to ServletContainerIntializer
  • Resource sharing
    • Static (e.g. images) and JSP resources no longer confined to document root of web application
    • May be place in WEB-INF/lib/<*.jar>/META-INF/resources
    • Resources in document root take precedence over those in bundled jars
  • Asynch support
    • Must declare @WebServlet(asyncSupported=true)
    • Then call AsyncContext ctx = ServletRequest.startAsync(req, res)
    • AsyncContext can then either:
      dispatch(String path)
      start(Runnable action)
    • Then paired with complete()
    • ServletRequest.isAsyncSupported()
      • True if all <filter|servlet> support async in the filter chain and the request dispatch chain
      • Can be configured in web.xml, Annotation, or API
  • Security
    • Added support for:
      // Declared using https contraints with @ServletSecurity
      HttpServletRequest.authenticate
      HttpServletRequest.login
      HttpServletRequest.logout
Document generated by Confluence on Feb 03, 2026 15:43